package com.example.restful.controller;

import com.example.restful.entity.User;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/user/")
public class UserController {
    public List<User> getUsers(){
        List<User> userList = new ArrayList<>();
        for(int i=0;i<10;i++){
            String name = "用户";
            String email = "user";
            String phone = "12378002222";
            User user=new User();
            user.setId(i);
            user.setName(name+i);
            user.setAge(18);
            user.setEmail(email+i+"@qq.com");
            user.setPassword("123456");
            user.setPhone(phone+i);

            userList.add(user);

        }
        return userList;
    }
    @GetMapping("/list")
    public List<User> getList(){
        return getUsers();
    }
    @GetMapping("/{id}")
    public User getUser(@PathVariable int id){
       return getUsers().get(id);

    }
    @PostMapping("/add")
    public List<User> addUser(@RequestBody User user){
        List<User> users = getUsers();
        users.add(user);
        return users;

    }

    @PostMapping("/update/{id}")
    public List<User> updateUser(@PathVariable Integer id,@RequestBody User user){
        List<User> users = getUsers();
        users.get(id).setName(user.getName());
        users.get(id).setAge(user.getAge());
        users.get(id).setEmail(user.getEmail());
        users.get(id).setPassword(user.getPassword());
        users.get(id).setPhone(user.getPhone());
        return users;

    }

    @PostMapping("/{id}")
    public List<User> delUser(@PathVariable Integer id){
        List<User> users = getUsers();
        User user = users.get(id);
        users.remove(user);
        return users;

    }

}
